package com.prs.crm.action.archives;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.archives.SecretLevel;
import com.prs.crm.service.archives.SecretLevelService;

public class SecretLevelAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private SecretLevel secretLevel;
	private SecretLevelService secretLevelService;
	private PaginationSupport pagination;
	public SecretLevel getSecretLevel() {
		return secretLevel;
	}
	public void setSecretLevel(SecretLevel secretLevel) {
		this.secretLevel = secretLevel;
	}
	public SecretLevelService getSecretLevelService() {
		return secretLevelService;
	}
	public void setSecretLevelService(SecretLevelService secretLevelService) {
		this.secretLevelService = secretLevelService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	@Action("listSecretLevel")
	public String listSecretLevel(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(SecretLevel.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSecretLevelService().listByPage(criteria, pagination));
		return SUCCESS;	
	}
	@Action("editSecretLevel")
	public String editSecretLevel(){
		if (ids!=null) {
			this.secretLevel=this.getSecretLevelService().get(ids[0]);
		}
		return SUCCESS;
	}
	
	@Action(value="saveSecretLevel",results={
			@Result(name="success",type="redirect",location="listSecretLevel"),
			@Result(name="error",type="chain",location="editSecretLevel")
	})
	public String saveSecretLevel(){
		this.getSecretLevelService().save(secretLevel);
		return SUCCESS;
	}
	
	@Action(value="removeSecretLevel",results={
			@Result(name="success",type="redirect",location="listSecretLevel"),
			@Result(name="error",type="chain",location="listSecretLevel")
	})
	public String removeSecretLevel(){
		if (ids!=null) {
			this.secretLevel=this.getSecretLevelService().get(ids[0]);
		}
		if (secretLevel.getArchivesManager().size()!=0) {
			this.addActionError("该条记录已被引用，不能删除！");
			return ERROR;
		}
		this.getSecretLevelService().batchRemove(ids);
		return SUCCESS;
	}
}
